package cs.sy.common.conn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;




public class DBConnection {
	private final Log log = LogFactory.getLog(DBConnection.class); 
	
	private static final String className = "com.mysql.jdbc.Driver"; 
	
//	private static final String url = "jdbc:mysql://localhost:3306/perfectorder?useUnicode=true&characterEncoding=utf-8";
	
	private static final String url = "jdbc:mysql://121.101.216.142:44944/perfectorder?useUnicode=true&characterEncoding=utf-8";
	
	private static final String user = "root";
	
	private static final String passwd = "qq5ys!!@#test";
	private static Connection connection;
	public Connection getCon() {
		try {
			log.error("url:"+url);
			Class.forName(className);
			try {
				connection = DriverManager.getConnection(url, user, passwd);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (ClassNotFoundException e) {
			
			e.printStackTrace();
			
		}
		return connection;
	}

	public void closeAll(ResultSet rs, PreparedStatement ps, Connection conn) {
		try {
			if (conn != null) {
				conn.close();
				conn = null;
			}
			if (rs != null) {
				rs.close();
			}
			if (ps != null) {
				ps.close();
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			ps = null;
			rs = null;
			conn = null;
			e.printStackTrace();
		} finally {
			ps = null;
			rs = null;
			conn = null;
		}
	}
}
